<template>
  <div class="container">
    <el-card shadow>
      <!-- 学科跳转过来 才显示面包屑导航 -->
      <div slot="header" class="clearfix" v-if="page.subjectID">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <!-- <template slot-scope="scope"> -->
          <el-breadcrumb-item>{{ sName }}</el-breadcrumb-item>
          <!-- </template> -->
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <!-- 头部搜索区域 -->
      <el-row type="flex" justify="space-between">
        <el-col>
          <el-form :inline="true" :model="page" ref="searchForm">
            <el-form-item label="目录名称">
              <el-input v-model="page.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="page.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clearFind">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="4">
          <el-row type="flex" justify="end">
            <el-button
              v-if="page.subjectID"
              @click="$router.push('/subjects/list')"
              type="text"
              icon="el-icon-back"
              >返回学科</el-button
            >
            <el-button type="success" icon="el-icon-edit" @click="directorysAdd"
              >新增目录</el-button
            >
          </el-row>
        </el-col>
      </el-row>

      <!-- 工具栏 -->
      <el-alert
        :title="`数据一共${page.counts}条`"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>

      <!-- 表格展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#f4f4f5' }"
        style="width: 100%; margin-top: 15px"
      >
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope"
            >{{ new Date(scope.row.addDate) | parseTime }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="disableClick(scope.row)"
              type="text"
              size="small"
              >{{ scope.row.state === 0 ? '开启' : '禁用' }}</el-button
            >
            <el-button
              @click="editHandleClick(scope.row)"
              type="text"
              size="small"
              :disabled="scope.row.state === 0 ? true : false"
              >修改</el-button
            >
            <el-button
              @click="DeleteHandleClick(scope.row)"
              type="text"
              size="small"
              :disabled="scope.row.state === 0 ? true : false"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          style="margin-top: 30px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          layout=" prev, pager, next, sizes,jumper"
          :total="page.counts"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 新增目录弹层组件 -->
    <directorysAdd
      :dialogVisible.sync="dialogVisible"
      :tableData="tableData"
      :subjectID="page.subjectID"
      @getDirectorysList="getDirectorysList"
    ></directorysAdd>

    <!-- 修改目录弹层 -->
    <el-dialog
      title="修改目录"
      :visible.sync="editDialogVisible"
      :before-close="editHandleCancel"
      width="30%"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="所属学科" v-if="!this.page.subjectID">
          <el-select
            v-model="editForm.subjectName"
            placeholder="请选择"
            style="width: 100%"
            @change="onSelect"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="editForm.directoryName"
            @blur="editForm.directoryName = $event.target.value.trim()"
            placeholder="请输入目录名称"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editHandleCancel">取 消</el-button>
        <el-button type="primary" @click="editBtnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, changeState, update, remove } from '@/api/hmmm/directorys.js'
import { simple } from '../../api/hmmm/subjects'
import directorysAdd from '../components/directorys-add.vue'
export default {
  name: 'directorys',
  components: { directorysAdd },
  data () {
    return {
      page: {
        directoryName: '',
        state: null,
        subjectID: null,
        counts: 0, // 总记录数
        page: 1, // 当前页数
        pagesize: 10 // 页大小
      },
      // 展示表单数据
      tableData: [
        {
          // subjectName: '', // 所属学科名
          // tagName: '', // 目录名称
          // creator: '', // 创建者
          // addDate: '', // 创建日期
          // state: '' // 状态
        }
      ],
      dialogVisible: false, // 控制弹层展示
      editDialogVisible: false, // 编辑弹层是否展示
      // 修改目录表单对象
      editForm: {
        subjectName: '',
        directoryName: '',
        subjectID: ''
      },
      // 修改目录校验规则
      editFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      sName: '',
      subjectList: []
    }
  },
  created () {
    this.getDirectorysList(this.$route.query.id)
    this.sName = this.$route.query.subjectName
  },
  methods: {
    // 获取目录列表数据
    async getDirectorysList (id) {
      this.page.subjectID = id
      const { data } = await list({ ...this.page })
      // console.log(data.items)
      this.tableData = data.items
      this.page.counts = data.counts
      this.page.pagesize = data.pagesize - 0
      this.page.page = data.page - 0
    },
    // 搜索
    async search () {
      // 如果是学科跳转过来的
      // if (this.page.subjectID) {
      //   this.searchForm.subjectID = this.page.subjectID
      // }
      this.getDirectorysList(this.page.subjectID)
      // const { data } = await list({ ...this.searchForm })
      // // console.log(data.items)
      // this.tableData = data.items
      // this.page.counts = data.counts
    },
    // 清除
    clearFind () {
      // this.searchForm = {}
      this.page.directoryName = ''
      this.page.state = null
    },

    // 新增目录弹层展示
    directorysAdd () {
      this.dialogVisible = true
    },

    // 操作： 禁用 修改 删除
    async disableClick (row) {
      let state = 1
      if (row.state === state) {
        state = 0
      }
      const { data: res } = await changeState({ id: row.id, state: state })
      if (res.success) {
        this.$message.success('操作成功！')
        row.state = state
      } else {
        this.$message.error('操作失败！')
      }
    },

    onSelect (value) {
      const sName = this.subjectList.filter((item) => {
        return item.label === value
      })
      this.editForm.subjectID = sName[0].value
      this.editForm.subjectName = sName[0].label
    },

    // 编辑弹层
    async editHandleClick (row) {
      // console.log(row)
      this.editForm = { ...row }
      // this.editForm.directoryName = row.directoryName
      // this.editForm.subjectName = row.subjectName
      // this.editForm.subjectID = row.subjectID

      const { data } = await simple()
      this.subjectList = data
      this.editDialogVisible = true
      // console.log(this.tableData)
    },
    editHandleCancel () {
      this.$refs.editFormRef.resetFields()
      this.editForm = { directoryName: '', subjectID: '' }
      this.editDialogVisible = false
    },
    // 编辑确定
    async editBtnOk () {
      console.log(this.editForm, 1)
      // console.log(this.editForm.subjectID)
      await this.$refs.editFormRef.validate()
      await update(this.editForm)
      this.$message.success('修改成功')
      if (this.page.subjectID) {
        this.getDirectorysList(this.page.subjectID)
      } else {
        // this.getDirectorysList()
        this.search()
      }
      this.editHandleCancel()
    },

    async DeleteHandleClick (row) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该目录, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const data = await remove({ id: row.id })
      if (data.status !== 200) {
        return this.$message.error('删除目录失败！')
      }
      await this.getDirectorysList()
      this.$message.success('删除目录成功！')
    },

    // 页面变化 条数 页数
    handleSizeChange (val) {
      this.page.pagesize = val
      // this.page.directoryName = this.searchForm.directoryName
      this.getDirectorysList(this.page.subjectID)
      // this.search()
    },
    handleCurrentChange (val) {
      this.page.page = val
      // this.page.directoryName = this.searchForm.directoryName
      this.getDirectorysList(this.page.subjectID)
      // this.search()
    }
  }
}
</script>

<style scoped lang="less"></style>
